/**
 * 
 */
package com.travelsky.app.persistence;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Version;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;


/**
 * 角色
 * @author zhougc
 *
 */
@Entity(name = "SECURITY_ROLE")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@org.hibernate.annotations.Table(appliesTo = "SECURITY_ROLE",comment="角色")
public class Role implements Serializable{
	
	private static final long serialVersionUID = -605933897362325184L;
	@Id
	@GeneratedValue
	@Column(name = "ROLE_ID", nullable = false, length = 18)
	private Long id;
	@Version
	@Column(name = "VERSION")
	private Long version;
	@Column(name = "NAME", nullable = false, unique = true, length = 100)
	private String name;
	

	@Column(name = "DESCRIPTION", nullable = false, length = 200)
	private String description;
	
	@ManyToMany(targetEntity = Resource.class, fetch = FetchType.LAZY)
    @JoinTable(name = "SECURITY_ROLE_RESOURCE", joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "RESOURCE_ID"))
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Resource> resources;
	
	/**
	 * The default constructor
	 */
	public Role() {
		
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the resources
	 */
	public Set<Resource> getResources() {
		return resources;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param resources the resources to set
	 */
	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Role))
			return false;
		Role other = (Role) obj;
		if (getName() == null) {
			if (other.getName() != null)
				return false;
		} else if (!getName().equals(other.getName()))
			return false;
		return true;
	}
}
